//
//  ShouyeViewController.m
//  Xiangmu
//
//  Created by 이경인 on 2017/10/11.
//  Copyright © 2017年 LIJINGREN. All rights reserved.
//

#import "ShouyeViewController.h"
#import "SDCycleScrollView.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "huodongTableViewCell.h"
#import "HDxiangqingViewController.h"
#import "DSxiangqingViewController.h"
#import "BSxiangqingViewController.h"
#import "LoginViewController.h"
#import "LunbotuViewController.h"
#import "HDAlertView.h"

@interface ShouyeViewController () <SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>{
 
    NSArray *dateArr;
    UITableView *tableview;
    NSMutableArray *imageArray;
    SDCycleScrollView *cycleScrollView;
    
}

@property (nonatomic,strong)NSString *abc;
@end


@implementation ShouyeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"SUPERSTAR";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *tuichu = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"tuichu.jpg"] style:UIBarButtonItemStyleDone target:self action:@selector(tuichu)];
    self.navigationItem.rightBarButtonItem = tuichu;
    
    dateArr = [NSArray array];

    //活动
    UIButton *huodong = [UIButton buttonWithType:UIButtonTypeCustom];
    huodong.frame = CGRectMake(60, 280, 38, 38);
    [huodong setImage:[UIImage imageNamed:@"huodong.jpg"] forState:UIControlStateNormal];
    [huodong addTarget:self action:@selector(huodong) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:huodong];

    UILabel *huodong1 = [[UILabel alloc]initWithFrame:CGRectMake(63, 237, 200, 200)];
    huodong1.text = @"活动";
    huodong1.font = [UIFont boldSystemFontOfSize:14];
    huodong1.textColor = [UIColor grayColor];
    [self.view addSubview:huodong1];
    
    //大师榜
    UIButton *dashi = [UIButton buttonWithType:UIButtonTypeCustom];
    dashi.frame = CGRectMake(167, 277, 44, 44);
    [dashi setImage:[UIImage imageNamed:@"dashi.jpg"] forState:UIControlStateNormal];
    [dashi addTarget:self action:@selector(dashi) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:dashi];
    
    UILabel *dashi1 = [[UILabel alloc]initWithFrame:CGRectMake(167, 237, 200, 200)];
    dashi1.text = @"大师榜";
    dashi1.font = [UIFont boldSystemFontOfSize:14];
    dashi1.textColor = [UIColor grayColor];
    [self.view addSubview:dashi1];
    
    //最近比赛
    UIButton *bisai = [UIButton buttonWithType:UIButtonTypeCustom];
    bisai.frame = CGRectMake(280, 285, 30, 30);
    [bisai setImage:[UIImage imageNamed:@"bisai.jpg"] forState:UIControlStateNormal];
    [bisai addTarget:self action:@selector(bisai) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bisai];
    
    UILabel *bisai1 = [[UILabel alloc]initWithFrame:CGRectMake(266, 237, 200, 200)];
    bisai1.text = @"最近比赛";
    bisai1.font = [UIFont boldSystemFontOfSize:14];
    bisai1.textColor = [UIColor grayColor];
    [self.view addSubview:bisai1];
    
    //分割线
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0,360, 380, 10)];
    line.backgroundColor=[UIColor colorWithRed:220.0f/255.0f green:248.0f/255.0f blue:255.0f/255.0f alpha:0.5];
    [self.view addSubview:line];
    
   
    
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 380, self.view.frame.size.width, 300) style: UITableViewStylePlain];
    tableview.delegate=self;
    tableview.dataSource=self;
    [self.view addSubview:tableview];
    
    _abc = @"1";
    
    [self initwithdate:@"http://172.24.10.175/workout/api.php/lists/mod/activity"];
    
    [self initWithcycleScrollViewDate];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
        static NSString *str = @"huodongTableViewCell";
        huodongTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:str];
        if (cell==nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:str owner:self options:nil]lastObject];
            NSDictionary *dict =dateArr[indexPath.row];
            if ([_abc isEqualToString:@"1"]) {
                cell.label.text = dict[@"activity_name"];
            }else if ([_abc isEqualToString:@"2"]){
                cell.label.text = dict[@"realname"];
            }else if ([_abc isEqualToString:@"3"]){
                cell.label.text = dict[@"name"];

            }
         
            [cell.image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://172.24.10.175/workout/Uploads/%@",dict[@"thumb"]]]];
        }
        return cell;
   
}

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dateArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(void)initwithdate:(NSString *)str{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        dateArr =responseObject;
        [tableview reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}
-(void)initWithcycleScrollViewDate{
    imageArray = [NSMutableArray array];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://172.24.10.175/workout/api.php/lists/mod/itempos/SessionID/2ul9sj8vlfofj122e1s98048k2/related/1/posid/1"
 parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *array = responseObject;
        for (NSDictionary *dict in array) {
            [imageArray addObject:[NSString stringWithFormat:@"http://172.24.10.175/workout/Uploads/%@",dict[@"thumb"]]];
        }
        UIScrollView *demoContainerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, 380, 220)];
        demoContainerView.contentSize = CGSizeMake(self.view.frame.size.width, 0);
        [self.view addSubview:demoContainerView];
        cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 20, 380, 200) imageURLStringsGroup:imageArray];
        cycleScrollView.infiniteLoop = NO;
        cycleScrollView.delegate = self;
        cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
       
        [demoContainerView addSubview:cycleScrollView];
        //轮播间隔
        cycleScrollView.autoScrollTimeInterval = 2.0;
        [self.view layoutSubviews];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];

}
- (void)huodong{
   [self initwithdate:@"http://172.24.10.175/workout/api.php/lists/mod/activity"];
    _abc = @"1";

}
-(void)dashi{
    [self initwithdate:@"http://172.24.10.175/workout/api.php/lists/mod/master"];
    _abc = @"2";

}
-(void)bisai{
    [self initwithdate:@"http://172.24.10.175/workout/api.php/lists/mod/contest"];
    _abc = @"3";
    
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     if ([_abc isEqualToString:@"1"]) {
         NSDictionary *dict = dateArr[indexPath.row];
         HDxiangqingViewController *huodongVC = [[HDxiangqingViewController alloc] init];
         huodongVC.ID = dict[@"id"];
         self.hidesBottomBarWhenPushed = YES;
         [self.navigationController pushViewController:huodongVC animated:YES];
         self.hidesBottomBarWhenPushed = NO;
     }else if ([_abc isEqualToString:@"2"]){
         NSDictionary *dict = dateArr[indexPath.row];
         DSxiangqingViewController *dashiVC = [[DSxiangqingViewController alloc] init];
         dashiVC.ID = dict[@"id"];
         self.hidesBottomBarWhenPushed = YES;
         [self.navigationController pushViewController:dashiVC animated:YES];
         self.hidesBottomBarWhenPushed = NO;
     }else if([_abc isEqualToString:@"3"]){
         NSDictionary *dict = dateArr[indexPath.row];
         BSxiangqingViewController *bisaiVC = [[BSxiangqingViewController alloc] init];
         bisaiVC.ID = dict[@"id"];
         self.hidesBottomBarWhenPushed = YES;
         [self.navigationController pushViewController:bisaiVC animated:YES];
         self.hidesBottomBarWhenPushed = NO;
     }
 
}
-(void)tuichu {
    HDAlertView *alertView = [HDAlertView alertViewWithTitle:@"提示" andMessage:@"确定要退出吗？"];
    
    [alertView addButtonWithTitle:@"确定" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            [self presentViewController:loginVC animated:YES completion:nil];
        NSLog(@"styleTwo 确定");
    }];
    
    [alertView addButtonWithTitle:@"取消" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        NSLog(@"styleTwo 取消");
    }];
    
    [alertView show];

    
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
   NSDictionary *dict =dateArr[index];
    LunbotuViewController *lunbotuVC = [[LunbotuViewController alloc] init];
    lunbotuVC.ID = dict[@"id"];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:lunbotuVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    NSLog(@"---点击了第%ld张图片", index);
}

@end
